package com.dhnsoft.education.service.order.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhnsoft.education.entity.edu.vo.CourseInfoVo;
import com.dhnsoft.education.entity.order.Order;
import com.dhnsoft.education.entity.userCenter.UcenterMember;
import com.dhnsoft.education.mapper.order.OrderMapper;
import com.dhnsoft.education.service.edu.EduCourseService;
import com.dhnsoft.education.service.order.OrderService;
import com.dhnsoft.education.service.userCenter.UcenterMemberService;
import com.dhnsoft.education.utils.OrderNoUtil;
import com.dhnsoft.education.vo.CourseWebVoOrder;
import com.dhnsoft.education.vo.UcenterMemberOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author 代浩楠
 * @since 2022-04-14
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {



    @Autowired
    private EduCourseService courseService;

    @Autowired
    private UcenterMemberService memberService;

    //生成订单的方法
    @Override
    public String createOrders(String courseId, String memberId) {
        //通过远程调用根据用户id获取用户信息
        UcenterMemberOrder userInfoOrder = getUserInfoOrder(memberId);
        //通过远程调用根据课程id获取课程信息
        CourseWebVoOrder courseInfoOrder = getCourseInfoOrder(courseId);

        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());//订单号
        order.setCourseId(courseInfoOrder.getId());//课程id
        order.setCourseTitle(courseInfoOrder.getTitle());//课程名称
        order.setCourseCover(courseInfoOrder.getCover());//课程封面
        order.setTeacherName("test");
        order.setTotalFee(courseInfoOrder.getPrice());//课程价格
        order.setMemberId(userInfoOrder.getId());//用户id
        order.setEmail(userInfoOrder.getEmail());//用户邮箱
        order.setNickname(userInfoOrder.getNickname());//用户名
        order.setStatus(0);//0是未支付，1是已支付
        order.setPayType(1);//1是微信 2是支付宝
        baseMapper.insert(order);


        return order.getOrderNo();
    }

    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){

        CourseInfoVo courseInfo = courseService.getCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);

        return courseWebVoOrder;
    }

    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
        //根据用户id来查询数据
        UcenterMember userInfo = memberService.getById(id);
        UcenterMemberOrder infoCommonVo = new UcenterMemberOrder();
        BeanUtils.copyProperties(userInfo,infoCommonVo);
        return infoCommonVo;
    }
}
